package cn.ibizlab.odoo.core.odoo_sale.service.impl;

import java.io.Serializable;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
import java.math.BigInteger;
import java.util.Collection;

import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.beans.BeanCopier;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import cn.ibizlab.odoo.core.client.model.Isale_order_template;
import cn.ibizlab.odoo.core.odoo_sale.domain.Sale_order_template;
import cn.ibizlab.odoo.core.odoo_sale.filter.Sale_order_templateSearchContext;
import cn.ibizlab.odoo.core.odoo_sale.service.ISale_order_templateService;
import cn.ibizlab.odoo.util.SearchResult;
import cn.ibizlab.odoo.util.log.IBIZLog;

/**
 * 实体[报价单模板] 服务对象接口实现
 */
@Slf4j
@IBIZLog
@Service
public class Sale_order_templateServiceImpl implements ISale_order_templateService {

    @Autowired
    private cn.ibizlab.odoo.core.client.service.Isale_order_templateClientService sale_order_templateClientService;

    @Override
    public boolean create(Sale_order_template sale_order_template) {
        boolean bOk = false ;
        Isale_order_template clientModel = this.convert2Model(sale_order_template,null);
        sale_order_templateClientService.create(clientModel) ;
        this.convert2Domain(clientModel,sale_order_template);
        bOk = true ;

        return bOk ;
    }

    @Override
    public boolean update(Sale_order_template sale_order_template) {
        boolean bOk = false ;
        Isale_order_template clientModel = this.convert2Model(sale_order_template,null);
        sale_order_templateClientService.update(clientModel) ;
        this.convert2Domain(clientModel,sale_order_template) ;
        bOk = true;
        return bOk ;
    }

    @Override
    public Sale_order_template get(Integer id) {
        Isale_order_template clientModel = sale_order_templateClientService.createModel();
        clientModel.setId(id);
        sale_order_templateClientService.get(clientModel) ;
        return this.convert2Domain(clientModel,null);
    }

    @Override
    public Sale_order_template get(Sale_order_template sale_order_template) {
        Sale_order_template retsale_order_template = this.get(sale_order_template.getId()) ;
    	BeanCopier copier=BeanCopier.create(retsale_order_template.getClass(), sale_order_template.getClass(), false);
        copier.copy(retsale_order_template, sale_order_template, null);
        return sale_order_template ;
    }

    @Override
    public void removeBatch(Collection<? extends Serializable> idList, int batchSize) {

        //sale_order_templateClientService.removeBatch(sale_order_templates) ;

    }

    @Override
    public void createBatch(List<Sale_order_template> sale_order_templates, int batchSize) {

                        //sale_order_templateClientService.createBatch(sale_order_templates,batchSize) ;

    }

    @Override
    public boolean remove(Integer id) {
        boolean bOk = false ;
        Isale_order_template clientModel = sale_order_templateClientService.createModel();
        clientModel.setId(id);
        sale_order_templateClientService.remove(clientModel) ;
        bOk = true ;
        return bOk ;
    }

    @Override
    public void updateBatch(List<Sale_order_template> sale_order_templates, int batchSize) {

        //sale_order_templateClientService.updateBatch(sale_order_templates,batchSize) ;

    }

    /**
     * 查询集合 默认查询
     */
    @Override
    public Page<Sale_order_template> searchDefault(Sale_order_templateSearchContext context) {
        List<Sale_order_template> list = new ArrayList<Sale_order_template>();
        Page<cn.ibizlab.odoo.core.client.model.Isale_order_template> clientModelList = sale_order_templateClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Isale_order_template clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Sale_order_template>(list, context.getPageable(), clientModelList.getTotalElements());
    }


    /**
     * 根据EMail模板查询
     */
    @Override
    public Page<Sale_order_template> selectByOdoo_mail_template(Integer id){
        Sale_order_templateSearchContext context = new Sale_order_templateSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Sale_order_template> list = new ArrayList<Sale_order_template>();
        Page<cn.ibizlab.odoo.core.client.model.Isale_order_template> clientModelList = sale_order_templateClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Isale_order_template clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Sale_order_template>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据用户查询
     */
    @Override
    public Page<Sale_order_template> selectByOdoo_create(Integer id){
        Sale_order_templateSearchContext context = new Sale_order_templateSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Sale_order_template> list = new ArrayList<Sale_order_template>();
        Page<cn.ibizlab.odoo.core.client.model.Isale_order_template> clientModelList = sale_order_templateClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Isale_order_template clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Sale_order_template>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据用户查询
     */
    @Override
    public Page<Sale_order_template> selectByOdoo_write(Integer id){
        Sale_order_templateSearchContext context = new Sale_order_templateSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Sale_order_template> list = new ArrayList<Sale_order_template>();
        Page<cn.ibizlab.odoo.core.client.model.Isale_order_template> clientModelList = sale_order_templateClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Isale_order_template clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Sale_order_template>(list, context.getPageable(), clientModelList.getTotalElements());

    }



    public Isale_order_template convert2Model(Sale_order_template domain , Isale_order_template model) {
        if(domain == null)
            return model ;
        if(model==null){
            model = sale_order_templateClientService.createModel() ;
        }

         if(domain.getNameDirtyFlag())
            model.setName(domain.getName());
         if(domain.getRequire_signatureDirtyFlag())
            model.setRequire_signature(domain.getRequire_signature());
         if(domain.getDisplay_nameDirtyFlag())
            model.setDisplay_name(domain.getDisplay_name());
         if(domain.getRequire_paymentDirtyFlag())
            model.setRequire_payment(domain.getRequire_payment());
         if(domain.getCreate_dateDirtyFlag())
            model.setCreate_date(domain.getCreate_date());
         if(domain.getNumber_of_daysDirtyFlag())
            model.setNumber_of_days(domain.getNumber_of_days());
         if(domain.get__last_updateDirtyFlag())
            model.set__last_update(domain.get__last_update());
         if(domain.getNoteDirtyFlag())
            model.setNote(domain.getNote());
         if(domain.getIdDirtyFlag())
            model.setId(domain.getId());
         if(domain.getSale_order_template_option_idsDirtyFlag())
            model.setSale_order_template_option_ids(domain.getSale_order_template_option_ids());
         if(domain.getSale_order_template_line_idsDirtyFlag())
            model.setSale_order_template_line_ids(domain.getSale_order_template_line_ids());
         if(domain.getWrite_dateDirtyFlag())
            model.setWrite_date(domain.getWrite_date());
         if(domain.getActiveDirtyFlag())
            model.setActive(domain.getActive());
         if(domain.getCreate_uid_textDirtyFlag())
            model.setCreate_uid_text(domain.getCreate_uid_text());
         if(domain.getMail_template_id_textDirtyFlag())
            model.setMail_template_id_text(domain.getMail_template_id_text());
         if(domain.getWrite_uid_textDirtyFlag())
            model.setWrite_uid_text(domain.getWrite_uid_text());
         if(domain.getWrite_uidDirtyFlag())
            model.setWrite_uid(domain.getWrite_uid());
         if(domain.getMail_template_idDirtyFlag())
            model.setMail_template_id(domain.getMail_template_id());
         if(domain.getCreate_uidDirtyFlag())
            model.setCreate_uid(domain.getCreate_uid());
        return model ;
    }

    public Sale_order_template convert2Domain( Isale_order_template model ,Sale_order_template domain) {
        if(model == null)
            return domain ;
        if(domain == null){
            domain = new Sale_order_template();
        }

        if(model.getNameDirtyFlag())
            domain.setName(model.getName());
        if(model.getRequire_signatureDirtyFlag())
            domain.setRequire_signature(model.getRequire_signature());
        if(model.getDisplay_nameDirtyFlag())
            domain.setDisplay_name(model.getDisplay_name());
        if(model.getRequire_paymentDirtyFlag())
            domain.setRequire_payment(model.getRequire_payment());
        if(model.getCreate_dateDirtyFlag())
            domain.setCreate_date(model.getCreate_date());
        if(model.getNumber_of_daysDirtyFlag())
            domain.setNumber_of_days(model.getNumber_of_days());
        if(model.get__last_updateDirtyFlag())
            domain.set__last_update(model.get__last_update());
        if(model.getNoteDirtyFlag())
            domain.setNote(model.getNote());
        if(model.getIdDirtyFlag())
            domain.setId(model.getId());
        if(model.getSale_order_template_option_idsDirtyFlag())
            domain.setSale_order_template_option_ids(model.getSale_order_template_option_ids());
        if(model.getSale_order_template_line_idsDirtyFlag())
            domain.setSale_order_template_line_ids(model.getSale_order_template_line_ids());
        if(model.getWrite_dateDirtyFlag())
            domain.setWrite_date(model.getWrite_date());
        if(model.getActiveDirtyFlag())
            domain.setActive(model.getActive());
        if(model.getCreate_uid_textDirtyFlag())
            domain.setCreate_uid_text(model.getCreate_uid_text());
        if(model.getMail_template_id_textDirtyFlag())
            domain.setMail_template_id_text(model.getMail_template_id_text());
        if(model.getWrite_uid_textDirtyFlag())
            domain.setWrite_uid_text(model.getWrite_uid_text());
        if(model.getWrite_uidDirtyFlag())
            domain.setWrite_uid(model.getWrite_uid());
        if(model.getMail_template_idDirtyFlag())
            domain.setMail_template_id(model.getMail_template_id());
        if(model.getCreate_uidDirtyFlag())
            domain.setCreate_uid(model.getCreate_uid());
        return domain ;
    }


}
